/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.stascore.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.FunctionType;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.ConditionImpl;

import com.automvc.common.jq.Result;

import com.aiteasoft.school.stascore.entity.StaScore;
import com.aiteasoft.school.stascore.service.StaScoreService;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-29 18:14:40
 */
@Service
public class StaScoreServiceImpl  implements StaScoreService{  

	@Autowired
	SuidRich suidRich;

	@Override
	public StaScore process(StaScore entity) { 
		//Add the business logic if need. 放一些公共设置
		
//		//TODO
//		entity.setClassno("201");
		
		return entity;
	}
	
	@Override
	public boolean isWrongBizForUpdate(Result result, StaScore entity) {
		//Add the business logic if need.

		return false;
	}

	@Override
	public List<StaScore> select(StaScore entity, int start, int size) {
		Condition condition = selectAndCountCondition();
		condition.start(start);
		condition.size(size);
		//Add the business logic if need.
		
		return suidRich.select(entity, condition);
	}

	@Override
	public int selectCount(StaScore entity) {
	Condition condition = selectAndCountCondition();
	/*		condition.selectFun(FunctionType.COUNT, "*");
		//Add the business logic if need.
		
		String count = suidRich.selectWithFun(entity, condition);
		int total = count == null ? 0 : Integer.parseInt(count);
		return total;*/
		
		List<StaScore> list=suidRich.select(entity, condition);
		return list==null? 0 : list.size();
	}
	
	private Condition selectAndCountCondition() {
		Condition condition=new ConditionImpl();
		
		condition
		.selectField("classno,term,examno,subject")
		.selectFun(FunctionType.MAX, "score","maxscore")
		.selectFun(FunctionType.AVG, "score","avgscore")
		.selectFun(FunctionType.MIN, "score","minscore");
		
		
		//TODO 根据用户角色添加对班级的过滤.
		
//		condition.op("status", Op.nq, "FIN"); //统计已完成状态的
		
		condition
		.groupBy("classno,term,examno,subjectno,subject")
		.orderBy("classno,term,examno,subjectno")
		;
		
		return condition;
	}
	

}